package com.atgugui.eduservice.service.impl;

import com.atgugui.eduservice.client.VodServiceClient;
import com.atgugui.eduservice.entity.EduVideo;
import com.atgugui.eduservice.mapper.EduVideoMapper;
import com.atgugui.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodServiceClient vodServiceClient; // 注入service-vod模块接口服务对象，调用其模块中的接口

    @Override
    public boolean updateVideo(EduVideo eduVideo) {
        int i = baseMapper.updateById(eduVideo);
        return i > 0 ? true : false;
    }

    // TODO: 删除视频
    @Override
    public boolean removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(videoQueryWrapper);
        List<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                videoIds.add(videoSourceId);
            }
        }
        vodServiceClient.deleteBatch(videoIds); // 删除阿里云远程服务器上存储的视频资源，但是本地数据库中的信息并没有删除
        // 删除本地数据库中的该视频的相关数据信息
        baseMapper.deleteById(courseId); // 根据课程id删除对应的数据
        return true;
    }
}
